/**
 * FileName: PackageSetMapper
 * Author:   秋水浮萍
 * Date:     2020/8/3 20:34
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.demo.mapper;

import com.demo.dto.*;
import com.demo.entity.*;

import com.demo.model.CacheModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 秋水浮萍
 * @create 2020/8/3
 * @since 1.0.0
 */
@Repository
public interface PackageSetMapper {

    @Select("select su.true_name, su.login_name, su.dep_id, sd.dep_name, su.role_id, cr.role_name,bs.create_user_id memberUserId\n" +
            "from sys_user su\n" +
            "         left join sys_department sd on su.dep_id = sd.id\n" +
            "         left join crm_role cr on su.role_id = cr.role_id\n" +
            "left join (select * from bus_members where package_id=#{packageId}) bs\n" +
            "on su.user_id=bs.create_user_id\n" +
            "where su.user_id = #{userId}")
    UserPermissionDto userDtoPermission(String packageId, String userId);

    @Select("select count(*) from bus_packages where package_id=#{packageId} and user_id=#{userId}")
    int userPermission(String packageId, String userId);


    @Update("update bus_packages_link\n" +
            "set priority=1,\n" +
            "    priority_time=#{priorityTime}\n" +
            "where package_id = #{packageId}\n" +
            "  and claims_number = #{claimsNumber}")
    Integer packageLinkUpdate(String priorityTime, String packageId, String claimsNumber);



    @Update("<script>" +
            "UPDATE bus_claims set state=#{state} where claims_number in" +
            "<foreach collection='createId' item='id' open='(' separator=', ' close=')'>" +
            "  #{id} " +
            "</foreach>" +
            "</script>")
    int updateBusId(String state, @Param("createId") String[] createId);


    @Update("UPDATE bus_packages set disabled=1 where package_id=#{packageId}")
    int fake(String packageId);


    //关联组包与标的
    @Insert("<script>" +
            "insert into bus_packages_link(package_id, claims_number,priority,priority_time) " +
            "VALUES " +
            " <foreach collection='claims' item='id'  index='index'  separator=',' >" +
            "       (#{packageId},#{id},#{index}+1,#{priorityTime})     " +
            "</foreach>" +
            "</script>")
    int addContract(@Param("claims") String[] claims, String packageId, String priorityTime);

    //删除所有关联
    @Delete("delete FROM bus_packages_link where package_id =#{packageId}")
    int deleteLink(String packageId);

    @Select("select claims_number from bus_packages_link where package_id =#{packageId}")
    List<String> claimNumber(String packageId);


    //删除 修改组包状态后被其余组包选中的标的
    @Delete("<script>" +
            "DELETE from bus_packages_link where claims_number in" +
            "<foreach collection='createId' item='id' open='(' separator=', ' close=')'>" +
            "  #{id} " +
            "</foreach>" +
            " and package_id!=#{packageId}" +
            "</script>")
    int deleteLinks(@Param("createId") String[] createId, String packageId);

    @Delete("delete from bus_packages where user_id=#{userId} and update_time is null")
    int deleteNull(String userId);


    //查询联系人
    @Select("<script>" +
            "select * from crm_partner where  create_user_id=#{createUserId} and disabled=0 " +
            "<if test='partnerType != null and partnerType!=\"\"'>" +
            " and partner_type = #{partnerType}" +
            "</if>" +
            "<if test='value != null and value!=\"\"'>" +
            " and (telephone like '%${value}%' or  name like '%${value}%' or telephone_list->'$[*].telephones' like '%${value}%')" +
            "</if>" +
            "</script>")
    @Results(id = "partnerMap", value = {
            @Result(column = "telephone_list", property = "telephoneList", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "over_area", property = "overArea", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "asset_info", property = "assetInfo", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "fund_info", property = "fundInfo", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "exit_info", property = "exitInfo", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "justice_info", property = "justiceInfo", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "peer_info", property = "peerInfo", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
            @Result(column = "org_info", property = "orgInfo", typeHandler = com.demo.typehandler.JsonTypeHandler.class),
    })
    List<Partner> contractAll(int partnerType, String value, String createUserId);

    @Select("<script>" +
            "select * from crm_partner where  create_user_id=#{createUserId} and disabled=0 " +
            "<if test='partnerType != null and partnerType!=\"\"'>" +
            " and partner_type = #{partnerType}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "partnerMap")
    List<Partner> contractList(int partnerType, String createUserId);

    @Select("select * from crm_partner where partner_id=#{partnerId} and disabled=0")
    Partner findPartners(String partnerId);


    //查询所有用户
    @Select("SELECT u.user_id ,u.true_name,d.dep_name from sys_user u inner join sys_department d on u.dep_id =d.id where u.disabled =0 AND d.disabled =0 and u.role_id!='002' and user_id!=#{u.userId} order by u.dep_id \n")
    List<SysUserDTO> findUser(String userId);

    @Select("SELECT u.user_id ,u.true_name,d.dep_name from sys_user u inner join sys_department d on u.dep_id =d.id where u.disabled =0 AND d.disabled =0 and u.role_id!='002'   order by u.dep_id \n")
    List<SysUserDTO> findLeader(String userId);

    @Select("<script>" +
            "select sys_department.dep_name,sys_user.user_id,sys_user.true_name from sys_user inner join sys_department on sys_user.dep_id=sys_department.id\n" +
            "where sys_user.disabled=0 and sys_department.dep_name  in " +
            "<foreach collection='dep' item='id' open='(' separator=', ' close=')'>" +
            "  #{id}" +
            "</foreach>" +
            "order by sys_department.id" +
            "</script>")
    List<SysUserDTO> menbers(@Param("dep") String[] dep);

    //添加项目成员
    @Insert("<script>" +
            "insert into bus_members(package_id, create_user_id) " +
            "VALUES " +
            " <foreach collection='createUserId' item='id'  separator=',' >" +
            "       (#{packageId},#{id})     " +
            "</foreach>" +
            "</script>")
    int members(String packageId, @Param("createUserId") String[] createUserId);

    @Delete("DELETE FROM bus_members where package_id =#{packageId}")
    Integer deleteMember(String packageId);

    //查询所有成员
    @Select("select s.user_id from bus_members b inner join sys_user s on b.create_user_id=s.user_id where package_id=#{packageId} and s.disabled=0")
    List<String> membersAll(String packageId);

    //删除成员
    @Delete("delete from bus_members where package_id=#{packageId}")
    int deletes(String packageId);

    @Select("SELECT dep_name from sys_department where disabled =0")
    List<String> department();

    @Select("select true_name from sys_user where user_id=#{userId} and disabled=0")
    String name(String userId);


    //显示成员姓名
    @Select("SELECT  su.user_id,su.true_name,s.dep_name FROM bus_members bm inner join sys_user su on bm.create_user_id =su.user_id inner join (select * FROM sys_department where disabled =0) s on s.id =su.dep_id \n" +
            "where bm.package_id=#{packageId} and su.disabled =0")
    List<SysUserDTO> teamName(String packageId);

    @Delete("delete from cache_data where package_id=#{packageId} ")
    Integer cacheDelete(String packageId);

    @Insert("insert into  cache_data (package_id,a,b,c) value(#{packageId},#{a},#{b},#{c})")
    Integer cacheAdd(String packageId, String a, String b, String c);

    @Select("select * from cache_data  where package_id=#{packageId}")
    CacheModel cacheFind(String packageId);

    //发起标的结项
    @Update("<script>" +
            "update bus_packages_link set knot=1,launch_time=#{time} where package_id=#{packageId} " +
            "and claims_number= #{claimsNumber} " +

            "</script>")
    Integer launchKnot(String packageId, String claimsNumber, String time);

    @Select("update bus_claims set knot=#{knot} where claims_number=#{claimsNumber}")
    Integer updateKnot(String knot, String claimsNumber);

    @Select("update bus_mortgage set knot=#{knot} where claims_number=#{claimsNumber}")
    Integer mortUpdateKnot(String knot, String claimsNumber);

    @Select("update bus_prorig set knot=#{knot} where claims_number=#{claimsNumber}")
    Integer prorigUpdateKnot(String knot, String claimsNumber);


    @Select("select su.user_id,su.true_name,bs.create_user_id from sys_user su left join" +
            " (select * from bus_members where package_id=#{packageId}) bs on\n" +
            "su.user_id=bs.create_user_id where su.disabled=0")
    List<MembersDTO> packageMembers(String packageId);


    @Select("select priority from bus_packages_link where claims_number=#{claimsNumber} and package_id = #{packageId}")
    int priority(String claimsNumber, String packageId);

    @Update("update bus_packages_link\n" +
            "set priority=#{priority},\n" +
            "    priority_time=#{priorityTime}\n" +
            "where claims_number = #{claimsNumber}\n" +
            "  and package_id = #{packageId}")
    Integer priUpdate(int priority, String priorityTime, String claimsNumber, String packageId);
}